package com.threeox.ormlibrary.entity;

import com.threeox.ormlibrary.annotation.create.Table;
import com.threeox.ormlibrary.impl.SqlExecutor;
import com.threeox.utillibrary.util.EmptyUtils;

import java.io.Serializable;
import java.util.List;

/** 
 *
 * @ClassName: BaseORM
 *
 * @Description: (基本的数据库对象)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/16 15:51 
 * 
 * @version 1.0
 */
@SuppressWarnings({ "unchecked", "serial" })
public class BaseORM implements Serializable {

	protected final String TABLENAME = this.getClass().getSimpleName();

	/**
	 * 查询集合
	 * 
	 * @return
	 */
	public List<? extends BaseORM> findAll() {
		Table table = getClass().getAnnotation(Table.class);
		String tableName = EmptyUtils.isNotEmpty(table.value()) ? table.value() : TABLENAME;
		String sql = "SELECT * FROM " + tableName;
        return SqlExecutor.newInstance().queryList(sql, getClass());
	}

	/**
	 * 保存对象
	 */
	public Long save(){
		Table table = getClass().getAnnotation(Table.class);
		String tableName = EmptyUtils.isNotEmpty(table.value()) ? table.value() : TABLENAME;
		return SqlExecutor.newInstance().save(this, tableName);
	}
	
	
}
